home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / netprn / netprint.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-05-08  |  6.0 KB  |  166 lines

  1. VERSION 2.00
  2. Begin Form Form1 
  3.    Caption         =   "NetPrint"
  4.    ClientHeight    =   3900
  5.    ClientLeft      =   2625
  6.    ClientTop       =   1635
  7.    ClientWidth     =   3840
  8.    Height          =   4305
  9.    Icon            =   NETPRINT.FRX:0000
  10.    Left            =   2565
  11.    LinkMode        =   1  'Source
  12.    LinkTopic       =   "Form1"
  13.    ScaleHeight     =   3900
  14.    ScaleWidth      =   3840
  15.    Top             =   1290
  16.    Width           =   3960
  17.    Begin Frame Frame1 
  18.       Caption         =   "Linked Printers"
  19.       Height          =   1872
  20.       Left            =   120
  21.       TabIndex        =   0
  22.       Top             =   1920
  23.       Width           =   3612
  24.       Begin CommandButton Disconnect 
  25.          Caption         =   "&Disconnect"
  26.          Height          =   372
  27.          Left            =   2400
  28.          TabIndex        =   5
  29.          Top             =   720
  30.          Width           =   1092
  31.       End
  32.       Begin ListBox LinkedPrinters 
  33.          Height          =   1395
  34.          Left            =   120
  35.          Sorted          =   -1  'True
  36.          TabIndex        =   4
  37.          Top             =   300
  38.          Width           =   2175
  39.       End
  40.    End
  41.    Begin Frame Frame2 
  42.       Caption         =   "Available Printers and Ports"
  43.       Height          =   1752
  44.       Left            =   120
  45.       TabIndex        =   6
  46.       Top             =   60
  47.       Width           =   3612
  48.       Begin CommandButton Connect 
  49.          Caption         =   "&Connect"
  50.          Height          =   372
  51.          Left            =   2400
  52.          TabIndex        =   3
  53.          Top             =   1260
  54.          Width           =   1092
  55.       End
  56.       Begin ListBox PrintPorts 
  57.          Height          =   1005
  58.          Left            =   2400
  59.          Sorted          =   -1  'True
  60.          TabIndex        =   2
  61.          Top             =   240
  62.          Width           =   1095
  63.       End
  64.       Begin ListBox NetPrinters 
  65.          Height          =   1395
  66.          Left            =   120
  67.          Sorted          =   -1  'True
  68.          TabIndex        =   1
  69.          Top             =   240
  70.          Width           =   2175
  71.       End
  72.    End
  73. 'NetPrint has been placed in the public domain by Art Krumsee
  74. 'CompuServe ID 76702,1526.  Internet address art.krumsee@osu.edu
  75. Declare Function WNetGetConnection Lib "User" (ByVal LocalDev$, ByVal rmtname$, buffsize%) As Integer
  76. Declare Function WNetAddConnection Lib "User" (ByVal NetPath As String, ByVal Password As String, ByVal LocalDev As String) As Integer
  77. Declare Function WNetCancelConnection Lib "User" (ByVal LocalDev As String, ByVal Force As Integer) As Integer
  78. Declare Function GetPrivateProfileString Lib "Kernel" (ByVal lpApplicationName As String, KeyName As Any, Default As Any, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
  79. Declare Function SendMessage% Lib "User" (ByVal hWnd%, ByVal wMsg%, ByVal wParam%, ByVal lParam&)
  80. Declare Function GetFocus% Lib "User" ()
  81. Declare Function PutFocus% Lib "User" Alias "SetFocus" (ByVal hWnd%)
  82. Const WM_USER = &H400
  83. Const LB_RESETCONTENT = WM_USER + 5
  84. Sub ClearListBox (Ctrl As Control)
  85.     hWndOld% = GetFocus()
  86.     Ctrl.SetFocus
  87.     X = SendMessage(GetFocus(), LB_RESETCONTENT, 0, 0)
  88.     Suc% = PutFocus(hWndOld%)
  89. End Sub
  90. Sub Connect_Click ()
  91.     'x% = MsgBox(printports.text)
  92.     If netprinters.listindex = -1 Or printports.listindex = -1 Then
  93.         X% = MsgBox("You must select a printer and a port and then press Connect.")
  94.     Else
  95.         Link_Printer$ = netprinters.text
  96.         Link_Port$ = printports.text
  97.         X% = WNetAddConnection(Link_Printer$, "", Link_Port$)
  98.         Select Case X%
  99.         Case 0
  100.             ClearListBox linkedPrinters
  101.             ClearListBox printports
  102.             findlinks
  103.         Case 6
  104.             n% = MsgBox("Password required.  Could not connect printer.")
  105.         Case 52
  106.             n% = MsgBox("Network Connection to that port already exists.")
  107.         Case Else
  108.             n% = MsgBox("Network Link Failed")
  109.         End Select
  110.     End If
  111. End Sub
  112. Sub Disconnect_Click ()
  113.     'x% = MsgBox(printports.text)
  114.     If linkedPrinters.listindex = -1 Then
  115.         X% = MsgBox("You must select a linked printer and then press Disconnect.")
  116.     Else
  117.         Linked_Port$ = Left$(linkedPrinters.text, 5)
  118.         X% = WNetCancelConnection(Linked_Port$, 0)
  119.         Select Case X%
  120.         Case 0
  121.             ClearListBox linkedPrinters
  122.             ClearListBox printports
  123.             findlinks
  124.         Case 48
  125.             n% = MsgBox("This printer port was not connected to a network device.")
  126.         Case 49
  127.             n% = MsgBox("Open files prevented the completion of this operation")
  128.         Case Else
  129.             n% = MsgBox("Network Disconnect Failed")
  130.         End Select
  131.     End If
  132. End Sub
  133. Sub findlinks ()
  134.     'Find out which printer ports are already linked
  135.     For X = 1 To 4
  136.         n% = 0
  137.         rmtname$ = String$(255, 0)
  138.         LocalDev$ = "LPT" + LTrim$(Str$(X)) + ":"
  139.         n% = WNetGetConnection(LocalDev$, rmtname$, 255)
  140.         If n% = 0 Then
  141.             rmtname$ = Left$(rmtname$, InStr(rmtname$, Chr$(0)) - 1)
  142.             linkedPrinters.AddItem LocalDev$ + "  " + rmtname$
  143.         Else
  144.             printports.AddItem LocalDev$
  145.         End If
  146.     Next X
  147.     printports.Refresh
  148. End Sub
  149. Sub Form_Load ()
  150.     'NetPrint has been placed in the public domain by Art Krumsee
  151.     'CompuServe ID 76702,1526.  Internet address art.krumsee@osu.edu
  152.     findlinks
  153.     'Get list of available printers from NETPRINT.ini
  154.     ret$ = String$(255, 0)
  155.     App_Name$ = "Netprint"
  156.     Key_Val = 0&
  157.     Def_Val$ = "No Printers Defined"
  158.     Ini_Name$ = "netprint.ini"
  159.     n% = GetPrivateProfileString(App_Name$, ByVal Key_Val, Def_Val$, ret$, 255, Ini_Name$)
  160.     While Left$(ret$, 1) <> Chr$(0)
  161.         rmtname$ = Left$(ret$, InStr(ret$, Chr$(0)) - 1)
  162.         netprinters.AddItem ret$
  163.         ret$ = Right$(ret$, Len(ret$) - InStr(ret$, Chr$(0)))
  164.     Wend
  165. End Sub
  166.